package cn.lena.idle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import cn.lena.idle.dao.UserDbHelper;
import cn.lena.idle.pojo.DTO.LoginUser;
import cn.lena.idle.pojo.DTO.Version;
import cn.lena.idle.pojo.User;
import cn.lena.idle.util.PhotoUtils;

public class RegistActivity extends AppCompatActivity {

    private TextView toLogin;
    private Button registBtn;

    private String regist_userId;
    private String regist_username;
    private String regist_password;
    private String regist_address;
    private String regist_phone;
    private ImageButton head;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regist);

        // 单击登录
        toLogin = (TextView)findViewById(R.id.toLogin);
        toLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(RegistActivity.this,LoginActivity.class);
                startActivity(intent);
            }
        });

        // 单击注册
        registBtn=(Button)findViewById(R.id.registBtn);
        registBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                registUser();
            }
        });

        // 单击图片按钮时，能够在相册中选择图片
        head=(ImageButton)findViewById(R.id.regist_head);
        head.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_PICK);
                intent.setType("image/*");
                startActivityForResult(intent,1);
            }
        });


    }

    // 注册
    public void registUser(){
        // 校验是否为空
        regist_userId=((EditText)findViewById(R.id.regist_userId)).getText().toString().trim();
        if (regist_userId.equals("")) {
            Toast.makeText(this, R.string.userIdError, Toast.LENGTH_LONG).show();
            return;
        }
        regist_username=((EditText)findViewById(R.id.regist_username)).getText().toString().trim();
        if (regist_username.equals("")) {
            Toast.makeText(this, R.string.usernameError, Toast.LENGTH_LONG).show();
            return;
        }
        regist_password=((EditText)findViewById(R.id.regist_pass)).getText().toString().trim();
        if (regist_password.equals("")) {
            Toast.makeText(this, R.string.passError, Toast.LENGTH_LONG).show();
            return;
        }
        regist_address=((EditText)findViewById(R.id.regist_address)).getText().toString().trim();
        regist_phone=((EditText)findViewById(R.id.regist_phone)).getText().toString().trim();

        // 添加数据
        User user=new User(regist_userId,regist_username,regist_password, PhotoUtils.ImageButtonToByte(head),regist_phone,regist_address);
        UserDbHelper user_db=new UserDbHelper(getApplicationContext(),UserDbHelper.DB_NAME,null, Version.USER);
        int result=user_db.addUser(user);
        System.out.println(result);
        if(result != 0) {
            Toast.makeText(this, "注册成功！", Toast.LENGTH_LONG).show();
            // 跳转到首页
            Intent intent=new Intent(RegistActivity.this,IndexActivity.class);
            startActivity(intent);
            // 注册成功后不应该再回退到注册页面
            RegistActivity.this.finish();
        }else {
            Toast.makeText(this, "服务器出错", Toast.LENGTH_LONG).show();
            return;
        }
    }

    /**
     * 选择图片后触发事件 t
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if (requestCode == 1) {
            //从相册返回的数据
            if (data != null) {
                //得到图片的全路径
                Uri uri = data.getData();
                head.setImageURI(uri);
            }
        }
    }

}
